package com.hlhj.funlike_businessqzxd.utils;

import android.app.Dialog;
import android.content.Context;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.ImageView;
import android.widget.TextView;

import com.hlhj.funlike_businessqzxd.R;
import com.pl.wheelview.WheelView;

import java.util.ArrayList;
import java.util.List;


public class TimeDialog {

    private Dialog dialog;
    private ImageView iv_losedialog;
    private TextView tv_sure;
    private WheelView dayWheelView;
    private WheelView hourWheelView;
    private WheelView minulteWheelView;
    private WheelView dayWheelView1;
    private WheelView hourWheelView1;
    private WheelView minulteWheelView1;
    private String startBuffer ="";
    private String endBuffer = "";

    private String day ="";
    private String hour ="";
    private String minulte ="";

    private String day1 ="";
    private String hour1 ="";
    private String minulte1 ="";
    public TimeDialog(final Context context) {

        dialog = new Dialog(context, R.style.myDialogStyle);
        Window window = dialog.getWindow();
        window.setGravity(Gravity.CENTER);
        dialog.setContentView(R.layout.select_time_dialog);
        iv_losedialog = (ImageView) window.findViewById(R.id.iv_losedialog);
        tv_sure = (TextView) window.findViewById(R.id.tv_sure);
        hourWheelView = (WheelView) window.findViewById(R.id.from_hour);
        hourWheelView.setPosition(WheelView.POSITION.LEFT);

        minulteWheelView = (WheelView) window.findViewById(R.id.from_minulte);
        minulteWheelView.setPosition(WheelView.POSITION.LEFT);



        hourWheelView1 = (WheelView) window.findViewById(R.id.from_hour1);
        hourWheelView1.setPosition(WheelView.POSITION.LEFT);

        minulteWheelView1 = (WheelView) window.findViewById(R.id.from_minulte1);
        minulteWheelView1.setPosition(WheelView.POSITION.LEFT);

        DisplayMetrics outMetrics = new DisplayMetrics();
        window.getWindowManager().getDefaultDisplay().getMetrics(outMetrics);
        int width = outMetrics.widthPixels;
        window.getAttributes().width = width;
        hourWheelView.setData(getHourData());
        hourWheelView.setCyclic(true);
        minulteWheelView.setData(getHourMiulte());
        minulteWheelView.setCyclic(true);

        hourWheelView1.setData(getHourData());
        hourWheelView1.setCyclic(true);
        minulteWheelView1.setData(getHourMiulte());
        minulteWheelView1.setCyclic(true);


        hourWheelView.setOnSelectListener(hourWheelViewlistener);
        minulteWheelView.setOnSelectListener(minluteWheelViewlistener);

        hourWheelView1.setOnSelectListener(hourWheelViewlistener1);
        minulteWheelView1.setOnSelectListener(minluteWheelViewlistener1);



        iv_losedialog.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                CloseDialog();
            }
        });
        tv_sure.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                List<String> listime = new ArrayList<String>();
                listime.add(hour+":"+minulte);
                listime.add(hour1+":"+minulte1);
                ((ResultCallBack)context).callback(listime);
                CloseDialog();
            }
        });
    }

    public void setDeault(String startime, String endtime){
        if(null!=startime&&null!=endtime){
            if(startime.contains(":")&&endtime.contains(":")){
                String[] start = startime.split(":");
                String[] end = endtime.split(":");
                String shour = start[0];
                String smin = start[1];
                String ehour = end[0];
                String emin = end[1];

                if(shour.length()==2) {
                    if (shour.contains("0")&&(!"20".equals(shour))&&(!"10".equals(shour))) {
                        shour = shour.substring(1, shour.length());
                    }
                    hourWheelView.setDefault(Integer.parseInt(shour));
                }
                if(smin.length()==2) {
                    if (smin.contains("0")&&(!"20".equals(smin))&&(!"10".equals(smin))) {
                        smin = smin.substring(1, smin.length());
                    }
                    minulteWheelView.setDefault(Integer.parseInt(smin));
                }


                if(ehour.length()==2) {
                    if (ehour.contains("0")&&(!"20".equals(ehour))&&(!"10".equals(ehour))) {
                        ehour = ehour.substring(1, ehour.length());
                    }
                    hourWheelView1.setDefault(Integer.parseInt(ehour));
                }
                if(emin.length()==2) {
                    if (emin.contains("0")&&(!"20".equals(emin))&&(!"10".equals(emin))) {
                        emin = emin.substring(1, emin.length());
                    }
                    minulteWheelView1.setDefault(Integer.parseInt(emin));
                }
            }
        }

    }

    public void show(){
        if(null!=dialog && !dialog.isShowing()){
            dialog.show();
        }
    }


    public interface ResultCallBack{
        void callback(List<String> result);
    }

    public void CloseDialog() {
        if (null != dialog && dialog.isShowing()) {
            dismiss();
        }
    }

    private ArrayList<String> getHourData() {
        ArrayList<String> list = new ArrayList<String>();
        for (int i = 0; i < 24; i++) {
            if (i < 10) {
                list.add("0" + i);
            } else {
                list.add(i + "");
            }

        }
        return list;
    }


    private ArrayList<String> getHourMiulte() {
        ArrayList<String> list = new ArrayList<String>();
        for (int i = 0; i < 60; i++) {
            if (i < 10) {
                list.add("0" + i);
            } else {
                list.add(i + "");
            }

        }
        return list;
    }
    /**
     * 关闭对话框
     */
    public void dismiss() {
        dialog.dismiss();
    }
    WheelView.OnSelectListener  dayWheelViewlistener = new WheelView.OnSelectListener() {
        @Override
        public void endSelect(int id, String text) {

        }

        @Override
        public void selecting(int id, String text) {
            day=text;

        }
    };

    WheelView.OnSelectListener  hourWheelViewlistener = new WheelView.OnSelectListener() {
        @Override
        public void endSelect(int id, String text) {

        }

        @Override
        public void selecting(int id, String text) {
            hour=text;

        }
    };
    WheelView.OnSelectListener  minluteWheelViewlistener = new WheelView.OnSelectListener() {
        @Override
        public void endSelect(int id, String text) {

        }

        @Override
        public void selecting(int id, String text) {
            minulte=text;

        }
    };


    WheelView.OnSelectListener  dayWheelViewlistener1 = new WheelView.OnSelectListener() {
        @Override
        public void endSelect(int id, String text) {

        }

        @Override
        public void selecting(int id, String text) {
            day1=text;
        }
    };

    WheelView.OnSelectListener  hourWheelViewlistener1 = new WheelView.OnSelectListener() {
        @Override
        public void endSelect(int id, String text) {

        }

        @Override
        public void selecting(int id, String text) {
            hour1=text;
        }
    };
    WheelView.OnSelectListener  minluteWheelViewlistener1 = new WheelView.OnSelectListener() {
        @Override
        public void endSelect(int id, String text) {

        }

        @Override
        public void selecting(int id, String text) {
            minulte1=text;
        }
    };
}